<template>

    <a-form ref="formRef" name="custom-validation" :model="formState" v-bind="layout" @finish="handleFinish">
        <a-row>
            <a-col :span="4">
                <a-form-item has-feedback label="岗位名称" name="position_name">
                    <a-input allow-clear v-model:value="formState.position_name" />
                </a-form-item>
            </a-col>

            <a-col :span="4">
                <a-form-item has-feedback label="所属部门" name="dept_id">
                    <a-select allow-clear v-model:value="formState.dept_id" style="width: 100%"
                        placeholder="请选择所属部门">
                        <a-select-option v-for="item in departmentForQueryList" :key="item.key" :value="item.key">
                            {{ item.name }}
                        </a-select-option>
                    </a-select>
                </a-form-item>
            </a-col>

            <a-col :span="4">
                <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
                    <a-button type="primary" html-type="submit">查询</a-button>
                    <!-- <a-button type="primary" style="margin-left: 10px" @click="handleAdd">新增</a-button> -->
                </a-form-item>
            </a-col>
        </a-row>

        <a-row>
            <a-col :span="24">
                <a-flex justify="flex-end">
                    <a-button type="primary" style="margin-left: 10px" @click="handleAdd">新增</a-button>
                </a-flex>
            </a-col>
        </a-row>
    </a-form>


    <a-row>
        <a-col :span="24">
            <a-table size="small" style="width: 100%;" :columns="columns" :data-source="data">
                <template #bodyCell="{ column, text, record }">
                    <template v-if="column.dataIndex === 'name'">
                        <a>{{ text }}</a>
                    </template>
                    <template v-if="column.dataIndex === 'action'">
                        <a @click="handleEdit(record)">编辑</a>
                        <a @click="handleDelete(record)">删除</a>
                    </template>
                </template>
            </a-table>
        </a-col>
    </a-row>

    <a-row>
        <a-drawer v-model:open="open" class="custom-class" root-class-name="root-class-name" style="color: red"
            :title="addFormState.position_id ? '编辑' : '新增'" placement="right">
            <a-form :rules="addRulesForm" ref="addFormRef" name="custom-validation" :model="addFormState"
                v-bind="layout" @finish="addFormState.position_id ? handleEditFinish($event) : handleAddFinish($event)">

                <a-form-item has-feedback label="岗位名称" name="position_name">
                    <a-input v-model:value="addFormState.position_name" />
                </a-form-item>
                <a-form-item has-feedback label="所属部门" name="dept_id">
                    <a-select v-model:value="addFormState.dept_id" style="width: 100%" placeholder="请选择所属部门">
                        <a-select-option v-for="item in departmentList" :key="item.key" :value="item.key">
                            {{ item.name }}
                        </a-select-option>
                    </a-select>
                </a-form-item>


                <a-form-item has-feedback>
                    <a-col :span="24">
                        <a-flex justify="flex-end">
                            <a-button type="primary" style="margin-left: 10px" html-type="submit">保存</a-button>
                        </a-flex>
                    </a-col>
                </a-form-item>
            </a-form>
        </a-drawer>
    </a-row>
</template>
<script setup>
import service from '@/api/modulesApi/position';
import { reactive, ref } from 'vue';
import { message } from 'ant-design-vue';
// 获取部门
const departmentList = ref([]);
const getDepartment = async () => {
    const res = await service.getDepartment()
    departmentList.value = res.data
}
getDepartment()

// 获取查询项部门
const departmentForQueryList = ref([]);
const getDepartmentForQuery = async () => {
    const res = await service.getDepartmentForQuery()
    departmentForQueryList.value = res.data
}

const columns = [
    {
        title: '岗位名称',
        dataIndex: 'position_name',
        key: 'position_name',
    },
    {
        title: '所属部门',
        dataIndex: 'dept_id',
        key: 'dept_id',
        customRender: ({ text, record }) => {
            return record.dept_name
        }
    },
    {
        title: '创建时间',
        dataIndex: 'create_time',
        key: 'create_time',
    },
    {
        title: '操作',
        dataIndex: 'action',
        key: 'action',
    },

];
const data = ref([]);


const formRef = ref();
const formState = reactive({
    position_name: '',
    dept_id: '',

});

const open = ref(false);

const addFormState = reactive({
    position_name: '',
    dept_id: '',
});
const addFormRef = ref();

const addRulesForm = {
    position_name: [{ required: true, message: '请输入岗位名称' }],
    dept_id: [{ required: true, message: '请选择所属部门' }],
};

const handleAdd = () => {
    addFormState.position_name = ''
    addFormState.dept_id = ''
    addFormState.position_id = ''
    open.value = true;
};

const handleAddFinish = async (values) => {
    await service.addPosition(values)
    message.success('新增成功')
    getPosition()
    addFormRef.value.resetFields();
    open.value = false;

};

const layout = {
    labelCol: { span: 7 },
    wrapperCol: { span: 14 },
};

const handleEdit = (record) => {
    addFormState.position_name = record.position_name
    addFormState.dept_id = record.dept_id
    addFormState.position_id = record.position_id
    open.value = true;
}

const handleEditFinish = async (values) => {
    await service.updatePosition({
        position_id: addFormState.position_id,
        position_name: values.position_name,
        dept_id: values.dept_id
    })
    message.success('编辑成功')
    getPosition()
    addFormRef.value.resetFields();
    open.value = false;
}

const handleDelete = async (record) => {
    await service.deletePosition({ position_id: record.position_id })
    message.success('删除成功')
    getPosition()
}

const handleFinish = (values) => {
    console.log(values, formState);
    getPosition()
};

const getPosition = async () => {
    const res = await service.getPosition(formState)
    data.value = res.data
    getDepartmentForQuery()

}
getPosition()

</script>